import { hasToken } from '@/utils/storage'
import React from 'react'
import { Route, Redirect } from 'react-router-dom'

export default function AutoRoute({ component: Component, ...rest }) {
	return (
		<Route
			{...rest}
			render={({location}) => {
				if (hasToken()) {
					return <Component></Component>
				} else {
					return (
						<Redirect
							// to传递对象, 第一个{} 为插值表达式
							to={{
								pathname: '/login',
                                state: {
                                    from: location,
                                }
							}}
						></Redirect>
					)
				}
			}}
		></Route>
	)
}
